<template>
  <div class="app-switch-content">
    <div
      @click="handleChangeSwitch"
      class="app-switch-container"
      :style="appSwitchContainerStyle"
      :class="{on:props.modelValue}">
      <div class="app-switch-btn" ref="switchBtn" :style="appSwitchBtnStyle"></div>
    </div>
    <input
      class="app-switch-input"
      type="checkbox"
      hidden
      :checked="props.modelValue"
      ref="input"
      :id="props.id"
      :name="props.name"
    >
  </div>
</template>
<script lang="ts">
export default {
  name: 'AppSwitch'
}
</script>
<script lang="ts" setup>

import { computed, ref } from 'vue'

interface Props {
  modelValue: boolean
  activeColor?: string
  inactiveColor?: string
  width?: string
  disabled?: boolean
  name?: string
  id?: string
}

const props = withDefaults(defineProps<Props>(), {
  modelValue: false,
  activeColor: '#409EFF',
  inactiveColor: '#EBEEF5',
  width: '40px',
  disabled: false,
  name: '',
  id: ''
})

const emit = defineEmits(['update:modelValue'])

const appSwitchContainerStyle = computed(() => `
        background: ${props.modelValue ? props.activeColor : props.inactiveColor};
        border-color:${props.modelValue ? props.activeColor : '#DCDFE6'};
        width:${props.width}
      `
)

const appSwitchBtnStyle = computed(() => `left:${props.modelValue ? 'calc(100% - 19px)' : ''}`)

const  handleChangeSwitch =()=> {
  emit('update:modelValue', !props.modelValue)
}
// export default {
//   props: {
//     value: {
//       type: [Boolean, String, Number],
//       default: false
//     }
//   },
//   data() {
//     return {}
//   },
//   computed: {
//
//   },
//   methods: {
//
//   },
//   watch: {
//     checked() {
//       this.$refs.input.checked = this.checked
//     }
//   }
// }
</script>

<style scoped lang="scss">

.app-switch-input {
  width: 100%;
  height: 100%;
  opacity: 0;
  position: absolute;
  cursor: pointer;
}

.app-switch-content {
  //display: flex;
  //align-items: center;
  display: inline-flex;
  vertical-align: middle;
  position: relative;
  height: 20px;
  justify-content: center;
}

.app-switch-container {
  height: 20px;
  width: 40px;
  background: #EBEEF5;
  border-radius: 20px;
  position: relative;
  cursor: pointer;
  box-sizing: border-box;
  border: 1px solid #DCDFE6;
  transition: .3s;

  .app-switch-btn {
    transition: .15s;
    position: absolute;
    width: 20px;
    height: 20px;
    top: -1px;
    left: -1px;
    border-radius: 10px;
    background: #ffffff;
    box-shadow: 0 1px 2px 2px rgba(0, 0, 0, 0.16);
  }
}
</style>
